package com.ygj.purchaseOrderApproval.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.base.common.json.JsonUtil;
import com.base.common.persistent.dao.CSgDAO;
import com.base.common.persistent.dao.CSglcmDAO;
import com.base.common.persistent.dao.CSgmDAO;
import com.ygj.base.common.persistent.model.CSg;
import com.ygj.base.common.persistent.model.CSglcm;
import com.ygj.base.common.persistent.model.CSgm;
import com.ygj.base.webservice.entity.purchaseOrder.CSgEntity;
import com.ygj.base.webservice.entity.purchaseOrder.CSglcmEntity;
import com.ygj.purchaseOrderApproval.service.PurchaseOrderApprovalService;

@Service("purchaseOrderApprovalService")
public class PurchaseOrderApprovalServiceImpl implements PurchaseOrderApprovalService{

	@Resource
	private CSglcmDAO cSglcmDAO;
	
	@Resource
	private CSgDAO cSgDAO;
	
	@Resource
	private CSgmDAO cSgmDAO;
	
	@Override
	public int queryPurchaseOrderApprovalTotalCounts(String fmen) {
		String sql = "";
		sql = "select * from c_sglcm where name = '"+ fmen +"' and 状态 = '审批' and skey like 'Gr%'";
		return cSglcmDAO.findByPropertiesAndPage(sql).size();
	}

	@Override
	public List<CSgEntity> queryPurchaseOrderApproval(String fmen, int page) {
		String sql = "";

		sql = "select top 15 model.* from (select ROW_NUMBER() OVER(order by csg.日期 desc) as rownum, csg.申购单号, csg.日期, csg.部门 from c_sglcm as csglcm join c_sg as csg on csglcm.skey = csg.申购单号 where csglcm.name = '" + fmen + "' and csglcm.状态  = '审批' and csglcm.skey like 'Gr%') as model where model.rownum >= " + (((page - 1) * 15) + 1);
		List<CSg> list = cSgDAO.findByPropertiesAndPage(sql);
		List<CSgEntity> cSgs = new ArrayList<CSgEntity>();
		for(CSg o : list) {
			CSgEntity cSgEntity = new CSgEntity();
			cSgEntity.set申购单号(String.valueOf(o.get申购单号()));
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			cSgEntity.setDate(dateFormat.format(o.get日期()));
			cSgEntity.set部门(String.valueOf(o.get部门()));
			cSgs.add(cSgEntity);
		}
		return cSgs;
	}

	@Override
	public CSgEntity queryPurchaseOrderApprovalDetail(String 申购单号) {
		CSgEntity cSgEntity = new CSgEntity();
		CSg cSg = cSgDAO.findBy申购单号(申购单号);
		Set<CSglcm> cSglcms = cSg.getcSglcms();
		List<CSglcm> sglcms = new ArrayList<CSglcm>();
		Iterator<CSglcm> iterator = cSglcms.iterator();
		while(iterator.hasNext()) {
			CSglcm cSglcm = iterator.next();
//			if("审批".equals(cSglcm.get状态().trim()) || "提交".equals(cSglcm.get状态().trim()) || "完成".equals(cSglcm.get状态().trim())){
				sglcms.add(cSglcm);
//			}
		}
		
		for(int i = 0; i < sglcms.size(); i++){
			for(int j = i + 1; j < sglcms.size(); j++){
				if(sglcms.get(i).getSn() > sglcms.get(j).getSn()){
					CSglcm cSglcm = sglcms.get(i);
					sglcms.set(i, sglcms.get(j));
					sglcms.set(j, cSglcm);
				}
			}
		}
		cSgEntity.setId(cSg.getId());
		cSgEntity.setcSgms(cSg.getcSgms());
//		cSgEntity.set日期(cSg.get日期());
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		cSgEntity.setDate(dateFormat.format(cSg.get日期()));
		cSgEntity.set状态(cSg.get状态());
		cSgEntity.set申购人(cSg.get申购人());
		cSgEntity.set申购单号(cSg.get申购单号());
		cSgEntity.set申购原因(cSg.get申购原因());
		cSgEntity.set类型(cSg.get类型());
		cSgEntity.set部门(cSg.get部门());
		cSgEntity.set部门代码(cSg.get部门代码());
		cSgEntity.set领料人(cSg.get领料人());
		
		List<CSglcmEntity> sglcmEntities = new ArrayList<CSglcmEntity>();
		for(CSglcm cSglcm : sglcms){
			CSglcmEntity cSglcmEntity = new CSglcmEntity();
			cSglcmEntity.setcSg(cSglcm.getcSg());
			cSglcmEntity.setDno(cSglcm.getDno());
			cSglcmEntity.setId(cSglcm.getId());
			cSglcmEntity.setName(cSglcm.getName());
			cSglcmEntity.setPs(cSglcm.getPs());
			SimpleDateFormat dateFormatSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			if(cSglcm.getPstime() != null){
				cSglcmEntity.setPstimeStr(dateFormatSimpleDateFormat.format(cSglcm.getPstime()));
			}
			cSglcmEntity.setSkey(cSglcm.getSkey());
			cSglcmEntity.setSn(cSglcm.getSn());
			cSglcmEntity.setZw(cSglcm.getZw());
			cSglcmEntity.set状态(cSglcm.get状态());
			sglcmEntities.add(cSglcmEntity);
		}
		cSgEntity.setcSglcmsList(sglcmEntities);
		return cSgEntity;
	}

	@Transactional
	@Override
	public int approve(String 申购单号, int isLastflag, String ps, int sn, String csgmListStr) {
		String sql = "";
		
		sql = "update c_sglcm set 状态  = '完成', ps = '" + ps + "', pstime = getdate() where 状态 = '审批' and sn = " + sn + " and skey = '" + 申购单号 + "'";
		if(cSglcmDAO.updateSomeByProperties(sql) > 0){
			if(isLastflag != 1){
				sql = "update c_sglcm set 状态 = '审批' where skey = '" + 申购单号 + "' and sn = " + (sn + 1);
				if(cSglcmDAO.updateSomeByProperties(sql) > 0){
					if(csgmListStr != null && !"".equals(csgmListStr.trim())) {
						List<String> cSgmsStr = JsonUtil.string2Obejct(csgmListStr, List.class);
						for(String cSgmStr: cSgmsStr){
							CSgm cSgm = JsonUtil.string2Obejct(cSgmStr, CSgm.class);
							cSgmDAO.update(cSgm);
						}
					}
					return 1;
				}
			} else {
				sql = "insert into c_sglcm (sn, zw, name, skey, 状态) select sn, zw, name, skey, '签收' from c_sglcm where skey = '" + 申购单号 + "'";
				if(cSglcmDAO.add(sql) > 0){
					if(csgmListStr != null && !"".equals(csgmListStr.trim())) {
						List<String> cSgmsStr = JsonUtil.string2Obejct(csgmListStr, List.class);
						for(String cSgmStr: cSgmsStr){
							CSgm cSgm = JsonUtil.string2Obejct(cSgmStr, CSgm.class);
							cSgmDAO.update(cSgm);
						}
					}
					return 1;
				}
			}
			throw new RuntimeException();
		}
		
		return 0;
	}

	@Transactional
	@Override
	public int backToTheDrafters(String 申购单号, int sn, String ps) {
		String sql = "";
		
		if(sn != 1){
			sql = "update c_sglcm set 状态 = '退单' , ps = '" + ps + "' where  skey = '" + 申购单号 + "' and sn = " + sn;
			if(cSglcmDAO.updateSomeByProperties(sql) > 0){
				sql = "update c_sglcm set 状态 = '退单' where  skey = '" + 申购单号 + "' and sn = 1";
				if(cSglcmDAO.updateSomeByProperties(sql) > 0){
					return 1;
				}
			}
			throw new RuntimeException();
		}
		return 0;
	}

	@Transactional
	@Override
	public int cancelTheOrderToLast(String 申购单号, int sn, String ps) {
		String sql = "";
		
		if(sn != 1){
			sql = "update c_sglcm set 状态 = '退单', ps = '" + ps + "' where  skey = '" + 申购单号 + "' and sn = " + sn;
			if(cSglcmDAO.updateSomeByProperties(sql) > 0){
				sql = "update c_sglcm set 状态 = '审批' where  skey = '" + 申购单号 + "' and sn = " + (sn - 1);
				if(cSglcmDAO.updateSomeByProperties(sql) > 0){
					return 1;
				}
			}
			throw new RuntimeException();
		}
		return 0;
	}
	
}
